import java.util.Scanner;

/**
 * Created with IntelliJ IDEA
 * Description：创建两个线程，一个调用wait，一个调用notify
 * User：chenshu
 * Date：2023-11-08
 * Time：09:42
 */
public class Demo2 {
    public static Object locker = new Object();
    public static void main(String[] args) {
        Thread waitTask = new Thread(() -> {
            synchronized (locker) {
                try {
                    System.out.println("wait开始");
                    locker.wait();
                    System.out.println("wait结束");
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        waitTask.start();

        Thread notifyTask = new Thread(() -> {
            Scanner scan = new Scanner(System.in);
            System.out.println("输入任意内容开始通知：");
            //next会阻塞，直到用户真正输入内容后
            scan.next();
            synchronized (locker) {
                System.out.println("notify开始");
                locker.notify();
                System.out.println("notify结束");
            }
        });
        notifyTask.start();
    }
}
